varying vec3 eye, normal, lightDir, color;
varying vec2 texcoord;
uniform sampler2D webcam;

vec3 mirrorDirection(vec3 normal, vec3 incoming)
{
	return incoming-normal*2*dot(incoming, normal);
}

void main()
{
    float NL = dot(normal,lightDir);   
    vec3 mirr = mirrorDirection(normal, eye);
    float theta = acos(mirr.x);
    float phi   = acos(mirr.z); 
    vec3 result = vec3(0);//NL * color;

    for(float i = -0.2; i<=0.2; i+=0.05)
    for(float j = -0.2; j<=0.2; j+=0.05)
    {
        float newNL = dot(normal, mirr);
        vec3 samp = texture2D(webcam, vec2(theta+i, phi+j)).xyz;
        result += color*newNL*samp;
    }

    vec4 envcolor = texture2D(webcam, texcoord);        
    gl_FragColor = vec4(result, 1.0);
    //gl_FragColor = vec4(envcolor.xyz, 1.0);
}
